﻿using System;
using NLog;
using NUnit.Framework;
using ScrappyDB.Attributes;
using ScrappyDB.BaseClasses;

namespace ScrappyDB.IntegrationTests.Core
{


    /// <summary>
    ///This is a test class for BasicPropertyTests and is intended
    ///to contain all BasicPropertyTests Unit Tests
    ///</summary>
    [TestFixture]
    public class AttributeTests
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        public static string TestString = "Test123";
        public static int TestInt = 1234;
        public static int TestNegativeInt = -5678;
        public static double TestDouble = 1234.4567890;
        public static double TestNegativeDouble = -5678.7654321;
        public static DateTime TestDate = DateTime.Now.ToUniversalTime();

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [TestFixtureSetUp]
        public  void Init()
        {
            Cleanup();
        }
        
        //Use ClassCleanup to run code after all tests in a class have run
        [TestFixtureTearDown]
        public static void MyClassCleanup()
        {
            Cleanup();
        }
        
        //Use TestInitialize to run code before running each test
        //[SetUp]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TearDown]
        //public void MyTestCleanup()
        //{
        //    Cleanup();
        //}

        private static void Cleanup()
        {
            try
            {
                Db db = new Db();
                var deleteme = db.Query<AttribTest>(true);
                foreach (var d in deleteme)
                {
                    db.Delete(d);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        #endregion

        [Test]
        public void BasicNonStandardItemNameTest()
        {
            Db target = new Db();
            var o = new AttribTest();
            var testId = Guid.NewGuid();
            o.Id = testId;
            o.TestString = TestString;
            target.SaveChanges(o);

            var actual = target.Find<AttribTest>(TestString);

            Assert.AreEqual(TestString, actual.TestString);
        }

        [Test]
        public void EntityNameAndAlternateItemNameTest()
        {
            Db target = new Db();
            var o = new AttribTest();
            var testId = Guid.NewGuid();
            o.Id = testId;
            o.TestString = TestString;
            target.SaveChanges(o);

            var actual = target.Find<AttribTestAlternateName>(TestString);

            Assert.AreEqual(TestString, actual.AlternateName);
        }


        [DomainName("ScrappyDB_UnitTests"), EntityName("AttribTest")]
        private class AttribTest 
        {
            public Guid Id { get; set; }
            [ItemName]
            public string TestString { get; set; }
        }

        [DomainName("ScrappyDB_UnitTests"), EntityName("AttribTest")]
        private class AttribTestAlternateName 
        {
            public Guid SomeRandomId { get; set; }
            [ItemName]
            public string AlternateName { get; set; }
        }
  
    }
}
